﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Agnes.Core;
using Agnes.Libaray;

namespace Agnes.MyDocsLib
{
    /// <summary>
    /// 新建同级目录命令
    /// </summary>
    public class NewSameLevelCatalogCmd
    {
        private static NewSameLevelCatalogCmd _instance;

        /// <summary>
        /// 返回唯一实例
        /// </summary>
        public static NewSameLevelCatalogCmd Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new NewSameLevelCatalogCmd();
                return _instance;
            }
        }

        private RoutedCommand _requery;

        /// <summary>
        /// 是否能执行
        /// </summary>
        public bool CanExcuted { get; set; }

        NewSameLevelCatalogCmd()
        {
            CanExcuted = true;
            _requery = new RoutedCommand("New Article ParentCatalog", typeof(NewSameLevelCatalogCmd));
        }

        /// <summary>
        /// 新建目录的路由命令
        /// </summary>
        public static RoutedCommand Requery
        {
            get
            {
                return Instance._requery;
            }
        }

        /// <summary>
        /// 新建目录执行代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Excuted(object sender, ExecutedRoutedEventArgs e)
        {
            Catalog newCatalog = Catalog.CreateAsChild();

            //如果选中的节点为空或者它的父节点也为空,那么将建立到首层目录下
            if (ArticleManager.Instance.SelectedNode == null
                || (ArticleManager.Instance.SelectedNode as Catalog).ParentCatalog == null)
            {
                newCatalog.Title = "新建目录";
                ArticleManager.Instance.Roots.Add(newCatalog);
                AgnesServiceManager.LogServ.Info("New Same Level ParentCatalog Cmd: Add A New ParentCatalog: " + newCatalog.Title + " in 我的知识库/");
            }
            else
            {
                //否则将目录加到它父节点下面.
                newCatalog.Title = "新建目录";
                (ArticleManager.Instance.SelectedNode as Catalog).ParentCatalog.Childs.Add(newCatalog);
                newCatalog.ParentCatalog = (ArticleManager.Instance.SelectedNode as Catalog).ParentCatalog;
                AgnesServiceManager.LogServ.Info("New Same Level ParentCatalog Cmd: Add A New ParentCatalog: " + newCatalog.Title + 
                    " in: " + newCatalog.GetPath());
            }

            ArticleManager.Instance.Save();
        }


        /// <summary>
        /// 判断是否能执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CanExcute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (AgnesServiceManager.AgnesSlnService.CurrentSln == null)
                e.CanExecute = false;
            else
                e.CanExecute = CanExcuted;
        }
    }
}
